//
//  EditInfoCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/25/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


final class EditDetailsCell: UITableViewCell {

    // MARK: - Outlets
    
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        lbl.textAlignment = .left
        lbl.sizeToFit()
        lbl.textColor = .black
        lbl.text = "Описание"
        return lbl
    }()
    
    private lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightBlueColor
        view.layer.cornerRadius = 10
        return view
    }()
    
    lazy var detailTextView: UITextView = {
        let textView = UITextView()
        textView.font = .systemFont(ofSize: 15)
        textView.textColor = .darkBlueColor
        textView.isScrollEnabled = false
        textView.sizeToFit()
        textView.backgroundColor = .clear
        return textView
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "dismiss"), for: .normal)
        return btn
    }()
    
    private lazy var separatorView = CellSeparator()
    
    
    // MARK: - Props
    
    var placeholder = "Напишите что-нибудь"
    
    // MARK: - Actions
    
    @objc private func clearText () {
        self.detailTextView.text = placeholder
    }
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension EditDetailsCell: ConfigurableCell {
    
    func configure(data object: String?, index: Int) {
        self.detailTextView.text = object ?? placeholder
    }

    func returnData() -> String? {
        return nil
    }
}


extension EditDetailsCell {
    
    private func addViews () {
        contentView.addSubview(containerView)
        contentView.addSubview(titleLbl)
        contentView.addSubview(separatorView)
        containerView.addSubview(closeBtn)
        containerView.addSubview(detailTextView)
        
        closeBtn.addTarget(self, action: #selector(clearText), for: .touchUpInside)
        
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
            make.height.equalTo(20)
        }
        
        separatorView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        containerView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(titleLbl.snp.bottom).offset(18)
            make.bottom.equalTo(separatorView.snp.top).offset(-16)
        }
        closeBtn.snp.makeConstraints { (make) in
            make.top.trailing.equalToSuperview().inset(16)
            make.width.height.equalTo(14)
        }
        detailTextView.snp.makeConstraints { (make) in
            make.top.equalTo(closeBtn.snp.bottom).offset(2)
            make.bottom.leading.trailing.equalToSuperview().inset(16)
            make.height.greaterThanOrEqualTo(40)
            make.height.lessThanOrEqualTo(200)
        }
    }
}

